﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace Region
{
    public partial class RegionRasporListForm : Form
    {
        private Engine m_engine;
        private RasporList m_rasporList;
        private RasporList m_rL_SB;
        private Int32 CurrentRasporID;
        private Boolean m_NoPodpis;        
        public Boolean NoPodpis { get { return m_NoPodpis; } set { m_NoPodpis = value; } }
        Properties.Settings m_ps;
        //String[] m_IspUserFIO;
        //Int32[] m_IspUserID;
        public bool EditRaspor(Int32 RasporID)
        {
            var RF = new Forms.RasporForm(m_engine, RasporID);
            RF.ShowDialog();
            this.ShowButton();
            return true;
        }
        public void ShowButton()
        {            
            //toolNew.Visible = true;
            menuNew.Visible = true;
            menuPodpis.Visible = false;
            menuRucPodpis.Visible = false;
            menuDelRucPodpis.Visible = false;
            menuNachPodpis.Visible = false;
            menuDelNachPodpis.Visible = false;
            if (CurrentRasporID > 0)
            {
                if (m_rL_SB == null) m_rL_SB = new RasporList(m_engine);
                m_rL_SB.setWhere("RasporID", CurrentRasporID);
                m_rL_SB.Filter();
                if (m_rL_SB.NumLines == 1)
                {
                    Region.RasporListLine line = m_rL_SB.getLine(0);
                    toolBase.Enabled = menuBase.Visible = true;
                    toolEdit.Enabled = menuEdit.Visible = true;
                    toolDel.Enabled = menuDel.Visible = !line.Podpis && !line.StatusVip;
                }
                else
                {
                    toolBase.Enabled = menuBase.Visible = false;
                    toolEdit.Enabled = menuEdit.Visible = true;
                    toolDel.Enabled = menuDel.Visible = false;
                }
            }
            else
            {
                toolBase.Enabled = menuBase.Visible =false;
                toolEdit.Enabled = menuEdit.Visible = true;
                toolDel.Enabled = menuDel.Visible = false;
            }
            
        }
        void loadIspUserList(Int32 OtdelID)
        {
            //String[] IspUserFIO;
            //Int32[] IspUserID;
            Dictionary<Int32, String> UserList = m_rasporList.IspUserList(OtdelID);
            //m_rasporList.IspUserList(out IspUserFIO, out IspUserID, OtdelID);
            IspUserList.Items.Clear();
            IspUserList.Items.Add(new Utils.ListItemValue("[Любой]", null));
            foreach (var User in UserList)
                IspUserList.Items.Add(new Utils.ListItemValue(User.Value, User.Key));
            //for (Int32 i = 0; i < IspUserFIO.Length; i++) IspUserList.Items.Add(new ListItemValue(IspUserFIO[i], IspUserID[i]));
            IspUserList.SelectedIndex = 0;
        }
        void loadOsnovList()
        {      
            OsnovIDList.Items.Clear();
            OsnovIDList.Items.Add(new Utils.ListItemValue("[Любое]", 0));
            foreach(var OsnovItem in m_engine.Osnov)
              OsnovIDList.Items.Add(new Utils.ListItemValue(OsnovItem.Value, OsnovItem.Key));
            OsnovIDList.SelectedIndex = 0;        
        }

        public RegionRasporListForm(Engine engine)
        {
            m_engine = engine;
            m_ps = Properties.Settings.Default;
            m_rasporList = new RasporList(m_engine);
            m_rL_SB = null;
            CurrentRasporID = 0;
            InitializeComponent();
            //Год проверки
            Int32 MinYear,MaxYear;
            m_rasporList.MinMaxRasporYear(out MinYear,out MaxYear);
            for (Int32 Y = MinYear; Y <= MaxYear;Y++) RasporYear.Items.Add(Y);
            RasporYear.Text = DateTime.Now.Year.ToString();
            //Строк на странице
            LineInPage.Text = m_ps.RasporReestrLineInPage.ToString();
            //Исполнитель
            loadIspUserList(18);
            //Список отделов
            Dictionary<Int32,Otdel> Otdels = m_engine.Otdels;
            foreach(var OtdelItem in Otdels)
              OtdelList.Add(OtdelItem.Value.ParentOtdelID,OtdelItem.Value.OtdelID,OtdelItem.Value.OtdelName);
            OtdelList.ShowTreeList();
            // Фильтрация по дате
            dateType.Items.Add(new Utils.ListItemValue("[Не фильтровать]","no"));
            dateType.Items.Add(new Utils.ListItemValue("Дата акта","AktDate"));
            dateType.Items.Add(new Utils.ListItemValue("Дата начала проверок", "Start"));
            dateType.Items.Add(new Utils.ListItemValue("Дата окончания проверок","End"));
            dateType.Items.Add(new Utils.ListItemValue("Дата контроля предписания", "ControlDate"));
            dateType.SelectedIndex = 0;
            // Основание
            loadOsnovList();

            this.ShowReestr(this,null);
            this.ShowButton();
        }
        public void ShowReestr(object sender, EventArgs e)
        {
            this.ReestrList.Items.Clear();
            m_rasporList.clearWhere();
            m_rasporList.setWhere("RasporYear", RasporYear.Text);
            if (checkPlan.Checked && !checkVP.Checked) m_rasporList.setWhere("CheckPlan", 1);
            if (!checkPlan.Checked && checkVP.Checked) m_rasporList.setWhere("CheckPlan", 0);
            String dateTypeValue = (String)((Utils.ListItemValue)dateType.SelectedItem).Value;
            if (dateStart.Checked && dateTypeValue != "no") m_rasporList.setWhere(dateTypeValue + "Start", dateStart.Value);
            if (dateEnd.Checked && dateTypeValue != "no") m_rasporList.setWhere(dateTypeValue + "End", dateEnd.Value);
            m_rasporList.setWhere("LineInPage", LineInPage.Text);
            m_rasporList.setWhere("RasporNumStart", RasporNumStart.Text);
            m_rasporList.setWhere("RasporNumEnd", RasporNumEnd.Text);
            if (IspUserList.SelectedIndex > 0) m_rasporList.setWhere("IspUserID", (Int32)((Utils.ListItemValue)IspUserList.SelectedItem).Value);
            //m_rasporList.setWhere("IspUserID", null); for(int i=0;i<m_IspUserFIO.Length;i++) if (m_IspUserFIO[i]==IspUserList.Text) {m_rasporList.setWhere("IspUserID", m_IspUserID[i]); break;}
            m_rasporList.setWhere("ParentOtdelID", OtdelList.SelectItemID());
            if (OsnovIDList.SelectedIndex > 0) m_rasporList.setWhere("OsnovID", (Int32)((Utils.ListItemValue)OsnovIDList.SelectedItem).Value);
            if (m_NoPodpis) m_rasporList.setWhere("NoPodpis", true);
            m_rasporList.Filter();
            Int32 Num = m_rasporList.NumLines;             
            for(int i=0;i<Num;i++)
            {
                Region.RasporListLine line = m_rasporList.getLine(i);
                ListViewItem R = new ListViewItem(line.RasporID.ToString());                
                R.SubItems.Add(line.RasporNum.ToString());
                R.SubItems.Add(line.OnlineID.ToString());
                R.SubItems.Add(line.Date.ToShortDateString());
                R.SubItems.Add(line.KontName);
                R.SubItems.Add(line.IspFIO);
                if (line.Podpis)
                {
                    R.SubItems.Add("Подписано");
                    R.Group = this.ReestrList.Groups["Podpis"];
                }
                else
                {
                    R.SubItems.Add("Не подписано");
                    R.Group = this.ReestrList.Groups["NPodpis"];
                }
                if (!line.StatusVip) R.Group = this.ReestrList.Groups["NVip"];
                this.ReestrList.Items.Add(R);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
        }
        private void Filter_Click(object sender, EventArgs e)
        {
            ShowReestr(sender, e);
        }

        private void RegionRasporListForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                //Сохранить Параметры окна
                Int32 RRLIP = m_ps.RasporReestrLineInPage;
                Int32.TryParse(LineInPage.Text,out RRLIP);
                m_ps.RasporReestrLineInPage = RRLIP;
                m_ps.Save();
            }
            finally { }
        }

        private void ReestrList_SelectedIndexChanged(object sender, EventArgs e)
        {            
            CurrentRasporID = 0;
            try
            {
                if (ReestrList.SelectedItems.Count > 0) CurrentRasporID = Int32.Parse(ReestrList.SelectedItems[0].Text);
            }
            catch { }
            ShowButton();
        }
        private void ZoomPlus_Click(object sender, EventArgs e)
        {
            Font = new Font(Font.FontFamily, (float)Math.Min(Font.Size * 1.2, 17.1));
        }
        private void ZoomMinus_Click(object sender, EventArgs e)
        {
            Font = new Font(Font.FontFamily, (float)Math.Max(Font.Size / 1.2, 6.875));
        }

        private void New_Click(object sender, EventArgs e)
        {
            EditRaspor(m_engine.newRaspor(0));
        }
        private void Base_Click(object sender, EventArgs e)
        {            
            if (CurrentRasporID > 0) EditRaspor(m_engine.newRaspor(CurrentRasporID));            
        }
        private void ReestrList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (CurrentRasporID != 0) EditRaspor(CurrentRasporID);
        }
        private void Edit_Click(object sender, EventArgs e)
        {
            if (CurrentRasporID != 0) EditRaspor(CurrentRasporID);
        }

        private void Del_Click(object sender, EventArgs e)
        {
            if (CurrentRasporID != 0) 
                if (m_engine.delRaspor(CurrentRasporID))
                    this.ShowReestr(this, null);
        }

        private void OtdelList_Load(object sender, EventArgs e)
        {

        }

        private void OtdelList_ItemChange(object sender, EventArgs e)
        {
            loadIspUserList((Int32)OtdelList.SelectItemID());
        }

        private void RasporNumStart_ValueChanged(object sender, EventArgs e)
        {
            if (RasporNumEnd.Value < RasporNumStart.Value) RasporNumEnd.Value = RasporNumStart.Value;
        }

        private void снятьПроверкуToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void toolStripContainer1_TopToolStripPanel_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
